package com.tnr.scgcxx.controller.safty;

import com.tnr.scgcxx.base.Constants;
import com.tnr.scgcxx.base.CurrUser;
import com.tnr.scgcxx.base.Result;
import com.tnr.scgcxx.dto.UserDto;
import com.tnr.scgcxx.exception.BusinessException;
import com.tnr.scgcxx.exception.SysException;
import com.tnr.scgcxx.service.safty.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/safty/login")
//@SessionAttributes(Constants.SESSION_ATTR_CURR_USER)//说明什么属性存储于Session
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/user")
    public Result login(@RequestBody UserDto dto,/* Model model*/ HttpSession session){
        try {
        CurrUser currUser=loginService.checkLogin(dto);

            //model默认在resquest中存储信息
            //model.addAttribute(Constants.SESSION_ATTR_CURR_USER,currUser);
            session.setAttribute(Constants.SESSION_ATTR_CURR_USER,currUser);

            return Result.success("登录成功！");
        } catch (BusinessException e) {
         return Result.fail(e.getMessage());
        }catch (SysException e){
            return Result.fail("系统繁忙......");
        }
    }


}
